package com.seisys.controller.system.head;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.seisys.controller.base.BaseController;
import com.seisys.service.system.menu.MenuService;
import com.seisys.service.system.user.UserService;
import com.seisys.util.AppUtil;
import com.seisys.util.Const;
import com.seisys.util.PageData;

/**
 * 类名称：HeadController 创建人：FH 创建时间：2014年8月16日
 * 
 * @version
 */
@Controller
@RequestMapping(value = "/head")
public class HeadController extends BaseController {

	@Resource(name = "userService")
	private UserService userService;
	@Resource(name = "menuService")
	private MenuService menuService;

	/**
	 * 获取头部信息
	 */
	@RequestMapping(value = "/getUname")
	@ResponseBody
	public Object getList() {
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();

			// shiro管理的session
			Subject currentUser = SecurityUtils.getSubject();
			Session session = currentUser.getSession();

			PageData pds = new PageData();
			pds = (PageData) session.getAttribute(Const.SESSION_userpds);

			if (null == pds) {
				String USERNAME = session.getAttribute(Const.SESSION_USERNAME)
						.toString(); // 获取当前登录者loginname
				pd.put("USERNAME", USERNAME);
				pds = userService.findByUId(pd);
				session.setAttribute(Const.SESSION_userpds, pds);
			}

			pdList.add(pds);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger, "");
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/**
	 * 获取返回路径URL
	 */
	@RequestMapping(value = "/getBackUrl")
	@ResponseBody
	public String getBackUrl() {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Map<String,Object> result = menuService.getMenuUrl();
			if(result != null && result.size() > 0)
			{
				map.put("menu", result);
				map.put("code", "success");
			}else
			{
				map.put("code", "fial");
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			map.put("code", "error");
		}
		return JSON.toJSONString(map);
	}
	
	/**
	 * 去编辑邮箱页面
	 */
	@RequestMapping(value = "/editEmail")
	public ModelAndView editEmail() throws Exception {
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		mv.setViewName("system/head/edit_email");
		mv.addObject("pd", pd);
		return mv;
	}

	/**
	 * 去代码生成器页面
	 */
	@RequestMapping(value = "/goProductCode")
	public ModelAndView goProductCode() throws Exception {
		ModelAndView mv = this.getModelAndView();
		mv.setViewName("system/head/productCode");
		return mv;
	}

}
